<?php

 /*
  +----------------------------------------------------------------------+
  | TagMoodRecord.class.php                                             |
  +----------------------------------------------------------------------+
  | Copyright (c) 2010 snda.com - Web Development Center                 |
  +----------------------------------------------------------------------+
  | TagMoodRecord model class                                           |
  +----------------------------------------------------------------------+
  | Authors: sebrina <zhangliyuan@snda.com>                              |
  +----------------------------------------------------------------------+
  |last modified: 2010-3-21 20:20										 |
  +----------------------------------------------------------------------+
*/

defined('TWITTER_DB_DEV_SERVER')|| define('TWITTER_DB_DEV_SERVER',true);
defined('TWITTER_DB_DEV_DEBUG') || define('TWITTER_DB_DEV_DEBUG',false);

defined('TWITTER_MCACHE_CHANNEL_NAME')|| define('TWITTER_MCACHE_CHANNEL_NAME','twitter'); //memcache分配的频道名
defined('TWITTER_MCACHE_EXPIRE_TIME')|| define('TWITTER_MCACHE_EXPIRE_TIME',60); //memcache 失效时间
defined('PDO_TAG_TAGNAME')|| define('PDO_TAG_TAGNAME','tagmood'); //pdo 的标签号
defined('TWITTER_MCACHE_EXPIRE_TIME')|| define('TWITTER_MCACHE_EXPIRE_TIME',60); //memcache 失效时间
defined('TWITTER_DB_PAGE_SIZE')|| define('TWITTER_DB_PAGE_SIZE',20); //分页每页的大小


require_once(dirname(__FILE__).'/../../config/UCConfTwitter.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibPDOFactory.php');
require_once(dirname(__FILE__).'/../../libraries/UCLibCache.php');




class UCModTagMoodRecord{
    private $_tagkey;
	private $_mkey_mood_list;
	private $_mkey_mood_count;
	private $_mkey_latest_mood;
	private $_cache;
	private  $_err_str='';



	function __construct($tagkey){
		$this->_tagkey = $tagkey;
		$this->_mkey_mood_list_1 = md5($tagkey.'_moodrecord_page_1');//第一页缓存的key
		$this->_cache = new UCLibCache(TWITTER_MCACHE_CHANNEL_NAME);
	}

	

	function __destruct(){
		$this->_cache->close();
	}

	/**
	 * @purpose: 统一获取错误信息
     * @return : 错误提示
	 */
	public function getErrorInfo() {
		return $this->_err_str;
	}
    

	
	/**
	 * @purpose: 取得tagkey的心情记录列表
	 * @return array or null
	 */
	function getTagMoodRecordList($page=1,$pagesize=TWITTER_DB_PAGE_SIZE){
		if($page < 1) $page = 1;
		if($page == 1){
		    $listinfo= $this->_cache->read($this->_mkey_mood_list_1);
			$this->_cache->close();
			if(!$listinfo)
			{
				$listinfo = $this->getDBAllTagMoodList($page,$pagesize);
                if ($listinfo) {
				   $this->_cache->write($this->_mkey_mood_list_1, $listinfo, TWITTER_MCACHE_EXPIRE_TIME);
				   $this->_cache->close();
			    } 
			}	
		}
		else
		{
			$listinfo = $this->getDBAllTagMoodList($page,$pagesize);
		}
		return $listinfo;
	}



	/**
	 * @purpose: 从索引 DB 中按页取得心情记录列表
	 * @return array or false
	 *  array(‘count'=>记录总数,
			  ‘page’=>当前页码,
			  ‘pages’=>总页数
			  ‘moodlist’=>array()
			 )
	 */
	function getDBAllTagMoodList($page,$pagesize){

		$pdo = UCLibPDOFactory::getPDO(PDO_TAG_TAGNAME,$this->_tagkey);
        $total_arr = $pdo->query_with_prepare("select count(*) as num from $pdo->database.$pdo->table where tag_key =?",array($this->_tagkey));
		//echo "\n total_arr  =";
		//var_dump($total_arr);
		if(is_array($total_arr)){
			$total=$total_arr[0]['num'];
		}else{
			$this->_err_str = "get list total failed";
			return false;
		}

        if($total ==0) return array();
       
		$total_pages = ceil($total /$pagesize);
	    if($page > $total_pages) $page = $total_pages;
		$start=($page - 1) * $pagesize;
		$list  = $pdo->query_with_prepare("select * from $pdo->database.$pdo->table where tag_key=? limit $start,$pagesize",array($this->_tagkey));
		//var_dump($list);
		
		if(!$list) {
			$this->_err_str = "get All Moodlist failed";
			return false;
		}

		$listinfo = array();
		$listinfo['count'] = $total;
		$listinfo['page'] = $page;
		$listinfo['pages'] = $total_pages;
        $listinfo['moodlist'] = $list;

		return $listinfo;
	}

}